import { assert } from "cc";
import { ISensor } from "./ISensor";
import { ObservationSpec } from "./ObservationSpec";

/**
 * 用于验证传感器形状是否与其他代理的传感器形状匹配。
 */
export class SensorShapeValidator {
    private _sensorShapes: ObservationSpec[] = null!;

    /**
     * 检查传感器列表的形状是否与之前的相同。
     * 如果这是第一个被检查的传感器列表，则保存其传感器大小。
     */
    public validateSensors(sensors: ISensor[]): void {
        if (!this._sensorShapes) {
            this._sensorShapes = new Array<ObservationSpec>(sensors.length);
            // 第一个代理，保存传感器大小
            for (let i = 0; i < sensors.length; i++) {
                this._sensorShapes[i] = sensors[i].getObservationSpec();
            }
        } else {
            // 检查与其他代理的传感器是否兼容
            if (this._sensorShapes.length !== sensors.length) {
                assert(
                    this._sensorShapes.length === sensors.length,
                    `传感器数量必须匹配。${this._sensorShapes.length} != ${sensors.length}`
                );
            }
            for (let i = 0; i < Math.min(this._sensorShapes.length, sensors.length); i++) {
                const cachedSpec = this._sensorShapes[i];
                const sensorSpec = sensors[i].getObservationSpec();
                if (cachedSpec.shape !== sensorSpec.shape) {
                    assert(
                        cachedSpec.shape === sensorSpec.shape,
                        `传感器形状必须匹配。${cachedSpec.shape} != ${sensorSpec.shape}`
                    );
                }
            }
        }
    }
}
